/**
 * @file : possionFactory.h
 * @author: fenghuwuyu
 * @version 1.0
 * @mail: 3148107006@qq.com
 * @date: 2021年06月22日 星期二 15时49分05秒
 * @brief:
 */

#ifndef MULTIGRIDFACTORY_H
#define MULTIGRIDFACTORY_H

#include <iostream>
#include <map>
#include "femsolver.h"


class femsolverFactory
{
	public:
		typedef femsolver*(*femsolverCreator)();
		femsolverCreator Find(std::string _method)
		{
			return femsolverMap.find(_method)->second;
		};

	private:
		typedef std::map<std::string, femsolverCreator> CallbackMap;
		CallbackMap femsolverMap;

    public:                                                                                              
        bool Registerfemsolver(const std::string &femsolverID, femsolverCreator createFn);
        bool Unregisterfemsolver(const std::string &femsolverID);                              
        femsolver* Createfemsolver(const std::string &femsolverID) const;                 
                                                                                                         
    // Singleton declare.                                                                                
    private:                                                                                             
        femsolverFactory(){};                                                                       
        static femsolverFactory *MyfemsolverFactory;                                           
                                                                                                         
    // Singleton declare.                                                                                
    public:                                                                                              
        static femsolverFactory *GetInstance()                                                      
        {                                                                                                
            femsolverFactory* MyfemsolverFactory = NULL;                                       
            if(MyfemsolverFactory == NULL)                                                          
            {                                                                                            
                MyfemsolverFactory = new femsolverFactory();                                   
            }                                                                                            
            return MyfemsolverFactory;                                                              
        };                                                                                               
        ~femsolverFactory(){femsolverMap.clear();};                                            
};

bool femsolverFactory::Registerfemsolver(const std::string &femsolverID, femsolverCreator createFn)
{
	return femsolverMap.insert(CallbackMap::value_type(femsolverID, createFn)).second;
}

bool femsolverFactory::Unregisterfemsolver(const std::string &femsolverID)
{
	return femsolverMap.erase(femsolverID) == 1;
}


#else
// DO NOTHING
#endif

